<template>
  <div class="register-page">
    <div class="scene-container">
      <div class="scene-content">
        <div class="ocean-scene">
          <div class="sun"></div>
          <div class="ocean"></div>
          <div class="boat"></div>
        </div>
        <div class="tagline">
          <h1>加入我们 守护海洋</h1>
          <p>共同守护蔚蓝深邃的海洋世界，让每一滴水都闪耀生命的光辉</p>
        </div>
      </div>
    </div>
    
    <div class="register-container">
      <div class="register-content">
        <div class="register-header">
          <div class="logo-wrapper">
            <img src="/images/logo.png" alt="Logo" class="logo">
          </div>
          <h2>创建账号</h2>
          <p>加入我们的海洋保护行动</p>
        </div>
        
        <el-form 
          ref="registerFormRef"
          :model="registerForm"
          :rules="rules"
          class="register-form">
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="用户名">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><User /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              show-password
              placeholder="密码">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><Lock /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              show-password
              placeholder="确认密码">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><Lock /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="name">
            <el-input
              v-model="registerForm.name"
              placeholder="真实姓名">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><Avatar /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="邮箱">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><Message /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              placeholder="手机号">
              <template #prefix>
                <div class="custom-input-icon">
                  <el-icon><Phone /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>

          <el-button 
            type="primary" 
            class="submit-btn"
            :loading="loading"
            @click="handleRegister">
            注册
          </el-button>

          <div class="login-hint">
            已有账号？ <router-link to="/login" class="login-link">立即登录</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { 
  User, Lock, Avatar, Message, Phone 
} from '@element-plus/icons-vue'

const router = useRouter()
const store = useStore()
const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  name: '',
  email: '',
  phone: ''
})

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await store.dispatch('register', {
          username: registerForm.username,
          password: registerForm.password,
          name: registerForm.name,
          email: registerForm.email,
          phone: registerForm.phone
        })
        ElMessage.success('注册成功！请登录您的账号')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '注册失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped lang="scss">
.register-page {
  display: flex;
  min-height: 100vh;
  background: #f5f7fa;
}

// 左侧场景
.scene-container {
  flex: 1;
  background: linear-gradient(135deg, #006994 0%, #003366 100%);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  border-radius: 8px 0px 0px 8px;

  .scene-content {
    width: 100%;
    max-width: 600px;
    position: relative;
    z-index: 1;
  }
}

.ocean-scene {
  position: relative;
  height: 300px;
  margin-bottom: 60px;

  .sun {
    position: absolute;
    top: 40px;
    left: 10%;
    width: 80px;
    height: 80px;
    background: #ffd700;
    border-radius: 50%;
    box-shadow: 0 0 60px #ffd700;
    animation: glow 4s ease-in-out infinite;
  }

  .ocean {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 150px;
    background: linear-gradient(0deg, #0077be 0%, #006994 100%);
    border-radius: 10px 10px 0 0;
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: -20px;
      left: 0;
      width: 200%;
      height: 40px;
      background: rgba(255, 255, 255, 0.3);
      border-radius: 40%;
      animation: wave 8s linear infinite;
    }
    
    &::after {
      content: '';
      position: absolute;
      top: -15px;
      left: -50%;
      width: 200%;
      height: 30px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 40%;
      animation: wave 10s linear infinite;
    }
  }

  .boat {
    position: absolute;
    bottom: 130px;
    left: 60%;
    width: 100px;
    height: 40px;
    background: #8B4513;
    border-radius: 5px 20px 0 0;
    transform: skew(20deg);
    animation: boat 15s ease-in-out infinite;
    
    &::before {
      content: '';
      position: absolute;
      top: -40px;
      left: 30px;
      width: 2px;
      height: 60px;
      background: #8B4513;
      transform: rotate(-10deg);
    }
    
    &::after {
      content: '';
      position: absolute;
      top: -45px;
      left: 20px;
      width: 40px;
      height: 40px;
      background: #fff;
      border-radius: 0 10px 0 10px;
      transform: rotate(45deg);
    }
  }
}

.tagline {
  color: white;
  text-align: center;
  
  h1 {
    font-size: 36px;
    margin-bottom: 20px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  }
  
  p {
    font-size: 18px;
    opacity: 0.8;
    line-height: 1.6;
  }
}

// 右侧注册表单
.register-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  
  .register-content {
    width: 100%;
    max-width: 440px;
  }
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
  
  .logo-wrapper {
    margin-bottom: 20px;
    
    .logo {
      width: 80px;
      height: auto;
    }
  }
  
  h2 {
    font-size: 28px;
    color: #333;
    margin-bottom: 10px;
  }
  
  p {
    color: #666;
    font-size: 16px;
  }
}

.register-form {
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .el-input {
    --el-input-height: 50px;
    --el-input-border-radius: 8px;
    --el-input-text-color: #333;
    --el-input-border: 1px solid #dcdfe6;
    --el-input-hover-border: 1px solid #c0c4cc;
    --el-input-focus-border: 1px solid #409eff;
    
    .el-input__wrapper {
      padding-left: 15px;
      box-shadow: none;
      transition: all 0.3s;
    }
  }
  
  .custom-input-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    color: #909399;
    margin-right: 8px;
  }
  
  .submit-btn {
    width: 100%;
    height: 50px;
    border-radius: 8px;
    font-size: 16px;
    margin-top: 10px;
    margin-bottom: 20px;
    background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
    border: none;
    
    &:hover {
      background: linear-gradient(135deg, #40a9ff 0%, #1890ff 100%);
    }
  }
  
  .login-hint {
    text-align: center;
    color: #606266;
    font-size: 14px;
    
    .login-link {
      color: #1890ff;
      text-decoration: none;
      font-weight: 500;
      
      &:hover {
        color: #40a9ff;
      }
    }
  }
}

@keyframes wave {
  0% {
    transform: translateX(0) scaleY(1);
  }
  50% {
    transform: translateX(-25%) scaleY(0.8);
  }
  100% {
    transform: translateX(-50%) scaleY(1);
  }
}

@keyframes boat {
  0%, 100% {
    transform: translateY(0) skew(20deg);
  }
  50% {
    transform: translateY(-15px) skew(20deg);
  }
}

@keyframes glow {
  0%, 100% {
    box-shadow: 0 0 60px #ffd700;
  }
  50% {
    box-shadow: 0 0 80px #ffd700;
  }
}

@media (max-width: 1200px) {
  .register-page {
    flex-direction: column;
  }
  
  .scene-container,
  .register-container {
    width: 100%;
    flex: none;
  }
  
  .scene-container {
    height: 300px;
    padding: 20px;
  }
  
  .ocean-scene {
    height: 150px;
    margin-bottom: 20px;
  }
  
  .tagline {
    h1 {
      font-size: 24px;
    }
    
    p {
      font-size: 14px;
    }
  }
}
</style> 